#include <bits/stdc++.h>

#define ll long long
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
const int INF = 1e9;
using namespace std;
/*思考区
 dp[i][j][k][z]为使用i张1 j张2 k张3 z张4个张数时得到的最大分数
*/
int dp[50][50][50][50];
int score[401] = {0};
int s[5] = {0};

void solve()
{
    int N, M;
    cin >> N >> M;
    for (int i = 1; i <= N; i++)
        cin >> score[i];
    for (int i = 1; i <= M; i++)
    {
        int x = 0;
        cin >> x;
        s[x]++;
    }
    dp[0][0][0][0] = score[1];

    for (int i = 0; i <= s[1]; i++)
    {
        for (int j = 0; j <= s[2]; j++)
        {
            for (int k = 0; k <= s[3]; k++)
            {
                for (int z = 0; z <= s[4]; z++)
                {
                    int maxx = 0;
                    int x = score[1 + i + 2 * j + 3 * k + 4 * z];
                    if (i != 0)
                        maxx = max(maxx, dp[i - 1][j][k][z] + x);
                    if (j != 0)
                        maxx = max(maxx, dp[i][j - 1][k][z] + x);
                    if (k != 0)
                        maxx = max(maxx, dp[i][j][k - 1][z] + x);
                    if (z != 0)
                        maxx = max(maxx, dp[i][j][k][z - 1] + x);
                    dp[i][j][k][z] = max(maxx, dp[i][j][k][z]);
                }
            }
        }
    }
    cout << dp[s[1]][s[2]][s[3]][s[4]] << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    solve();
    return 0;
}